package cn.iocoder.yudao.module.lowcode.dal.mysql.script;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.lowcode.controller.admin.script.vo.GroovyScriptPageReqVO;
import cn.iocoder.yudao.module.lowcode.dal.dataobject.script.GroovyScriptDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Groovy 脚本 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface GroovyScriptMapper extends BaseMapperX<GroovyScriptDO> {

    default PageResult<GroovyScriptDO> selectPage(GroovyScriptPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<GroovyScriptDO>()
                .likeIfPresent(GroovyScriptDO::getScriptName, reqVO.getScriptName())
                .likeIfPresent(GroovyScriptDO::getDisplayName, reqVO.getDisplayName())
                .eqIfPresent(GroovyScriptDO::getStatus, reqVO.getStatus())
                .eqIfPresent(GroovyScriptDO::getCategory, reqVO.getCategory())
                .eqIfPresent(GroovyScriptDO::getActive, reqVO.getActive())
                .betweenIfPresent(GroovyScriptDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(GroovyScriptDO::getSort)
                .orderByDesc(GroovyScriptDO::getId));
    }

    default List<GroovyScriptDO> selectListByStatus(Integer status) {
        return selectList(new LambdaQueryWrapperX<GroovyScriptDO>()
                .eq(GroovyScriptDO::getStatus, status)
                .eq(GroovyScriptDO::getActive, true)
                .orderByAsc(GroovyScriptDO::getSort)
                .orderByDesc(GroovyScriptDO::getId));
    }

    default GroovyScriptDO selectByScriptName(String scriptName) {
        return selectOne(new LambdaQueryWrapperX<GroovyScriptDO>()
                .eq(GroovyScriptDO::getScriptName, scriptName)
                .eq(GroovyScriptDO::getActive, true));
    }

    default List<GroovyScriptDO> selectActiveScripts() {
        return selectList(new LambdaQueryWrapperX<GroovyScriptDO>()
                .eq(GroovyScriptDO::getStatus, 1) // 启用状态
                .eq(GroovyScriptDO::getActive, true)
                .orderByAsc(GroovyScriptDO::getSort));
    }

    /**
     * 更新脚本执行统计
     */
    @Update("UPDATE lowcode_groovy_script SET execute_count = execute_count + 1, last_execute_time = #{executeTime} WHERE id = #{id}")
    void updateExecuteCount(@Param("id") Long id, @Param("executeTime") LocalDateTime executeTime);

    default List<GroovyScriptDO> selectByCategory(String category) {
        return selectList(new LambdaQueryWrapperX<GroovyScriptDO>()
                .eq(GroovyScriptDO::getCategory, category)
                .eq(GroovyScriptDO::getStatus, 1)
                .eq(GroovyScriptDO::getActive, true)
                .orderByAsc(GroovyScriptDO::getSort));
    }

}